package com.clsz.service.impl;

import com.clsz.annotation.RedisCache;
import com.clsz.bean.bo.CinemaSearchBo;
import com.clsz.bean.po.Brand;
import com.clsz.bean.po.CinemaInfo;
import com.clsz.bean.po.SpecialHallInfo;
import com.clsz.mapper.BrandMapper;
import com.clsz.mapper.SpecialHallMapper;
import com.clsz.service.CinemaService;
import com.clsz.utils.ESResponse;
import com.clsz.utils.ESUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CinemaServiceImpl implements CinemaService {
    @Resource
    private BrandMapper brandMapper;
    @Resource
    private SpecialHallMapper specialHallMapper;
    @Resource
    private ESUtils esUtils;
    @Override
    public List<CinemaInfo> getCinemaList() {
        return null;
    }

    @RedisCache(duration = 60 * 60)
    @Override
    public List<Brand> getBrandList() {
        return brandMapper.selectList(null);
    }

    @Override
    public List<SpecialHallInfo> getSpecialHallList() {
        return specialHallMapper.selectList(null);
    }

    /**
     * es搜索电影院
     * @param cinemaSearchBo
     * @return
     */
    @Override
    public List<CinemaInfo> searchCinema(CinemaSearchBo cinemaSearchBo) {
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        //品牌
        if (cinemaSearchBo.getBrandId() != null) {
            boolQueryBuilder.must(QueryBuilders.termQuery("brand_id", cinemaSearchBo.getBrandId()));
        }
        //放映厅类型
        if (cinemaSearchBo.getSpecialHallId() != null) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("cma_halls_query", cinemaSearchBo.getSpecialHallId()));
        }
        //
        if (cinemaSearchBo.getServiceId() != null) {
            boolQueryBuilder.must(QueryBuilders.termQuery("cma_service_query", cinemaSearchBo.getServiceId()));
        }
        //关键字
        if (cinemaSearchBo.getKeyword() != null && !cinemaSearchBo.getKeyword().isBlank()) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("cma_name", cinemaSearchBo.getKeyword()));
        }
        searchSourceBuilder.query(boolQueryBuilder);
        //排序
        if (cinemaSearchBo.getOrderColumn() != null && !cinemaSearchBo.getOrderColumn().isBlank()) {
            searchSourceBuilder.sort(
                    cinemaSearchBo.getOrderColumn(),
                    cinemaSearchBo.getOrderType() != null && cinemaSearchBo.getOrderType().equalsIgnoreCase("desc") ? SortOrder.DESC : SortOrder.ASC);
        }
        //设置分页
        searchSourceBuilder.from(cinemaSearchBo.getStartIndex());
        searchSourceBuilder.size(cinemaSearchBo.getPageSize());
        //发送请求
        ESResponse<CinemaInfo> searchList = esUtils.search("cinema",searchSourceBuilder, CinemaInfo.class);
        //设置查询命中数
        cinemaSearchBo.setResultCount((int)searchList.getResCount());
        return searchList.getData();
    }
}
